package com.tzy.common.utils;

import java.util.Random;
import java.util.UUID;

public class RandomUtil {
    public static void main(String[] args) {
        System.out.println("piecesPosition=" + getRandomNumber(5));
    }

    /**
     * 获取随机数
     *
     * @param times
     * @return
     */
    public static String getRandomNumber(int times) {
        StringBuffer piecesPosition = new StringBuffer();
        Random random = new Random();
        //随机生成碎片,碎片位置:第几个碎片
        String index = null;
        if (times > 1) {//次数大于1次
//            System.out.println("次数大于1次");
            for (int i = 0; i < times; i++) {
                index = String.valueOf(random.nextInt(9) + 1);
                //如果当前 i是最后一次
//                if( i < times-1 )
                piecesPosition.append(index + ",");
//                else
//                    piecesPosition.append(index);
            }
            return piecesPosition.toString();
        }
        //单抽一次
        index = (random.nextInt(9) + 1) + ",";
        return index;
    }

    /**
     * 随机生成六位数字验证码
     *
     * @return
     */
    public static String getCheckCode() {
//        String ziMu = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGJKLZXCVBNM1234567890";
        String ziMu = "1234567890";
        String result = "";
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            int index = random.nextInt(ziMu.length());
            char c = ziMu.charAt(index);
            result += c;
        }
        return result;
    }

    /**
     * 随机生成八位邀请码,英文字母+数字
     *
     * @return
     */
    public static String getInvitationCode() {
        String ziMu = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGJKLZXCVBNM1234567890";
//        String ziMu = "1234567890";
        String result = "";
        Random random = new Random();
        for (int i = 0; i < 8; i++) {
            int index = random.nextInt(ziMu.length());
            char c = ziMu.charAt(index);
            result += c;
        }
        return result;
    }

    /**
     * 随机生成八位邀请链接,英文字母+数字
     *
     * @return
     */
    public static String getInviteLink() {
        String ZiMu = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGJKLZXCVBNM1234567890";
//        String ZiMu = "1234567890";
        String result = "";
        Random random = new Random();
        for (int i = 0; i < 8; i++) {
            int index = random.nextInt(ZiMu.length());
            char c = ZiMu.charAt(index);
            result += c;
        }
        return "/" + result;

    }

    /**
     * 生成token用于登录验证
     *
     * @return
     */
    public static String genToken() {
        String s = UUID.randomUUID().toString();
        return s.replaceAll("-", "");
    }
}
